<template>
  <div class="article-page">
    <van-nav-bar fixed placeholder>
      <template #left>
        <van-tabs
          color="#000"
          title-inactive-color="#aaa"
          line-width="12px"
          @click="onTabsClick"
        >
          <van-tab title="推荐"></van-tab>
          <van-tab title="最新"></van-tab>
        </van-tabs>
      </template>
      <template #right>
        <div class="logo">
          <img src="@/assets/logo.png" alt="" />
        </div>
      </template>
    </van-nav-bar>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="我也是有底线的"
      @load="onLoad"
    >
      <article-item
        :article="item"
        v-for="item in list"
        :key="item.id"
      ></article-item>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from "@/components/ArticleItem.vue";
import { getArticalsAPI } from "@/api/artical";
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      current: 0,
      pageTotal: 1,
      sorter: "weight_desc",
    };
  },
  components: { ArticleItem },
  // async created() {
  // const res = await getArticalsAPI({ current: 1 });
  // console.log(res);
  // this.list = res.data.data.rows;
  // },
  methods: {
    async onLoad() {
      this.current++;
      const res = await getArticalsAPI({
        current: this.current,
        sorter: this.sorter,
      });
      this.list = [...this.list, ...res.data.data.rows];
      this.pageTotal = res.data.data.pageTotal;
      // 加载结束状态
      this.loading = false;
      // 如果所有数据没有了，修改finished为true，停止更新了
      if (this.current >= this.pageTotal) {
        this.finished = true;
      }
    },
    onTabsClick(index) {
      if (index === 1) {
        // 点击展示最新
        this.sorter = null;
      } else if (index === 0) {
        // 点击展示推荐
        this.sorter = "weight_desc";
      }
      (this.current = 0), (this.list = []), this.onLoad();
    },
  },
};
</script>

<style lang="less" scoped >
.article-page {
  padding-bottom: 40px;
  .logo {
    flex: 1;
    display: flex;
    justify-content: flex-end;
    > img {
      width: 64px;
      height: 28px;
      display: block;
      margin-right: 10px;
    }
  }
}
.article-item {
  .head {
    display: flex;
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
    .con {
      flex: 1;
      overflow: hidden;
      padding-left: 10px;
      p {
        margin: 0;
        line-height: 1.5;
        &.title {
          width: 280px;
        }
        &.other {
          font-size: 10px;
          color: #999;
        }
      }
    }
  }
  .body {
    font-size: 14px;
    color: #666;
    line-height: 1.6;
    margin-top: 10px;
  }
  .foot {
    font-size: 12px;
    color: #999;
    margin-top: 10px;
  }
}
</style>